<template>
  <div class="user">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form
          :loading="loading"
          :inline="true"
          :model="form"
          ref="form"
          class="demo-form-inline"
        >
          <el-form-item label="资源名称" prop="name">
            <el-input v-model="form.name" placeholder="输入资源名称"></el-input>
          </el-form-item>
          <el-form-item label="资源路径" prop="url">
            <el-input v-model="form.url" placeholder="输入资源路径"></el-input>
          </el-form-item>
          <el-form-item label="活动区域" prop="region">
            <el-select v-model="form.region" placeholder="活动区域">
              <el-option label="区域一" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button :disabled="loading" type="primary" @click="onSubmit"
              >查询</el-button
            >
            <el-button :disabled="loading" @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table border :data="userData" style="width: 100%">
        <el-table-column type="index" label="编号" width="180">
        </el-table-column>
        <el-table-column prop="name" label="资源名称"></el-table-column>
        <el-table-column prop="url" label="资源路径"></el-table-column>
        <el-table-column label="操作" width="200">
          <template>
            <el-button size="mini">编辑</el-button>
            <el-button size="mini" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :disabled="loading"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.current"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="form.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from "vue";
import { getUserPages } from "@/services/user";
import { Form } from "element-ui";

export default Vue.extend({
  name: "UserIndex",
  data() {
    return {
      loading: false,
      form: {
        name: "",
        url: "",
        region: "",
        current: 1,
        size: 5,
      },
      total: 0,
      userData: [],
    };
  },
  created() {
    this.getUserData();
  },
  methods: {
    async getUserData() {
      this.loading = true;
      const {
        data: {
          code,
          data: { records, total },
        },
      } = await getUserPages(this.form);
      if (code === "000000") {
        this.userData = records;
        this.total = total;
      }
      this.loading = false;
    },
    onReset() {
      (this.$refs.form as Form).resetFields();
    },
    onSubmit() {
      this.getUserData();
    },
    handleSizeChange(val: number) {
      this.form.size = val;
      this.form.current = 1;
      this.getUserData();
    },
    handleCurrentChange(val: number) {
      this.form.current = val;
      this.getUserData();
    },
  },
});
</script>

<style lang="scss" scoped></style>
